package com.cn.netty.basic;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

@Slf4j
public class EchoClientHandler extends SimpleChannelInboundHandler {
  @Override
  protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
    ByteBuf buf = (ByteBuf) msg;
    log.info("response -> {}", buf.toString(StandardCharsets.UTF_8));
//    ChannelFuture close = ctx.channel().close();

  }

  @Override
  public void channelActive(ChannelHandlerContext ctx) {
    for (int i = 0; i < 2; i++) {
      String s = "i`m client " + i + EchoServer.property;
      ctx.writeAndFlush(
              Unpooled.copiedBuffer(s.getBytes(StandardCharsets.UTF_8)));
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }

      log.info("send {}", s);

    }

  }

}
